<template>
	<section>
		<!--工具条-搜索区域-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 2px;">
			<el-form :inline="true" :model="queryFrom">
				<el-form-item>
					<el-input v-model="queryFrom.keyword" placeholder="菜单名称"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" v-on:click="getMenu1">查询</el-button>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="handleAdd">新增</el-button>
				</el-form-item>
			</el-form>
		</el-col>

		<!--列表-数据区域-->
		<el-table :data="list" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
			<el-table-column type="selection" width="45">
			</el-table-column>
			<el-table-column type="index" width="50">
			</el-table-column>
			<el-table-column prop="name" label="菜单名称" width="200" sortable>
			</el-table-column>
      <el-table-column prop="url" label="菜单路径" width="300" sortable>
      </el-table-column>
      <el-table-column prop="icon" label="菜单图标" width="200" sortable>
      </el-table-column>
			<el-table-column label="操作" width="150">
				<template scope="scope">
					<el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
					<el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
				</template>
			</el-table-column>
		</el-table>

		<!--工具条-批量删除-->
		<el-col :span="24" class="toolbar">
			<el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
      <el-pagination
          style="float:right;"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page=this.queryFrom.currentPage
          :page-sizes="[3, 5, 10, 20]"
          :page-size=this.queryFrom.pageSize
          layout="total, sizes, prev, pager, next, jumper"
          :total=this.total>
      </el-pagination>
		</el-col>

		<!--编辑界面-->
		<el-dialog title="新增/编辑"  :visible.sync="saveFormVisible" :close-on-click-modal="false">
			<el-form :model="saveForm" label-width="80px" :rules="saveFormRules" ref="saveForm">
				<el-form-item label="菜单名称" prop="name">
					<el-input v-model="saveForm.name" auto-complete="off"></el-input>
				</el-form-item>
        <el-form-item label="菜单路径" prop="url">
          <el-input v-model="saveForm.url" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="菜单图标" prop="icon">
          <el-input v-model="saveForm.icon" auto-complete="off"></el-input>
        </el-form-item>

			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="saveFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="saveSubmit" :loading="editLoading">提交</el-button>
			</div>
		</el-dialog>

	</section>
</template>

<script>

	export default {
		data() {
			return {
			  queryFrom:{
			    pageSize:10,
          currentPage:1,
          keyword:""
        },
				filters: {
					name: ''
				},
        list: [],
				total: 0,
				page: 1,
				listLoading: false,
				sels: [],
        //编辑界面不显示
				saveFormVisible: false,
				editLoading: false,
				saveFormRules: {
					name: [
						{ required: true, message: '请输入名称', trigger: 'blur' }
					]
				},
				//编辑界面数据
				saveForm: {
					id:null,
          name:"",
          email:"",
          age:"",
				},
        menus:[],
			}
		},
		methods: {
		  //handleSizeChange
      handleSizeChange(val){
        //设置页面条数的时候会被触发，val是显示条数
        this.queryFrom.pageSize = val;
        this.getMenu()
      },
			handleCurrentChange(val) {
        this.queryFrom.currentPage = val;
				this.getMenu();
			},
			//获取菜单列表
			getMenu() {
				this.listLoading = true;
        // 发送请求 请求后台
        this.$http.post("/menu",this.queryFrom)
            .then(res=>{
          // console.log(res)
          res = res.data.data
          this.list = res.list
          this.total = res.total
          console.log(res)
          this.listLoading = false;
          })
            .catch(res=>{
           })
			},
      //根据条件查询数据
      getMenu1() {
        //跳转第一页
        this.queryFrom.currentPage=1;
        this.listLoading = true;
        // 发送请求 请求后台
        this.$http.post("/menu",this.queryFrom)
            .then(res=>{
              // console.log(res)
              res = res.data.data
              this.list = res.list
              this.total = res.total
              console.log(res)
              this.listLoading = false;
            })
            .catch(res=>{
            })
      },
			//删除
			handleDel: function (index, row) {
				this.$confirm('确认删除该记录吗?', '提示', {
					type: 'warning'
				}).then(() => {
					// 发送请求
          this.$http.delete("/menu/"+row.id)
          .then(res=>{
            if (res.data.success){
              this.$message({type:"success",message:"删除成功"})
              this.queryFrom.currentPage = 1;
              this.getMenu()
            }
          }).catch(()=>{})
				}).catch(() => {

				});
			},
			//显示编辑界面
			handleEdit: function (index, row) {
				this.saveFormVisible = true;
        row = Object.assign({}, row)
				this.saveForm = row

			},
			//显示新增
			handleAdd: function () {
        this.saveFormVisible = true;
				this.saveForm={
          id:null,
          email:"",
          age:"",
        }
			},
			// 新增和修改
			saveSubmit: function () {
				this.$refs.saveForm.validate((valid) => {
					if (valid) {
						this.$confirm('确认提交吗？', '提示', {}).then(() => {
              // 发送后台请求
              this.$http.put("/menu",this.saveForm)
                  .then(res=>{
                    if (res.data.success){
                      this.$message({type:"success",message:"保存成功"})
                      this.saveFormVisible = false;
                      this.queryFrom.currentPage = 1;
                      this.getMenu()
                    }
                  }).catch(()=>{})
						});
					}
				});
			},

			selsChange: function (sels) {
				this.sels = sels;
			},
			//批量删除
			batchRemove: function () {
				var ids = this.sels.map(item => item.id);
				this.$confirm('确认删除选中记录吗？', '提示', {
					type: 'warning'
				}).then(() => {
          this.$http.patch("/menu",ids)
              .then(res=>{
                if (res.data.success){
                  this.$message({type:"success",message:"删除成功"})
                  this.queryFrom.currentPage = 1;
                  this.getMenu()
                }
              }).catch(()=>{})
				}).catch(() => {

				});
			}
		},
		mounted() {
			this.getMenu();
		}
	}

</script>

<style scoped>

</style>